package com.jin.concurrent.example.singleton;

import com.jin.concurrent.annoations.NotRecommand;
import com.jin.concurrent.annoations.NotThreadSafe;

/**
 * @author JinLiu
 * <p>
 * 懒汉模式: 单例实例在第一次使用时进行创建
 */
@NotThreadSafe
@NotRecommand
public class SingletonExample4 {

    // 私有构造函数
    private SingletonExample4() {
    }

    // 单例对象 volatile: 避免指令重排
    private volatile static SingletonExample4 instance = null;

    // 静态工厂方法
    public static synchronized SingletonExample4 getInstance() {
        if (null != instance) { // 双重检测
            synchronized (SingletonExample4.class) { // 同步锁
                if (null != instance) {
                    instance = new SingletonExample4();
                }
            }
        }

        return instance;
    }

}
